<template>
<div>
    <el-card style="padding:10px;background:#EBEEF5">
    <div style="margin: 10px 0;width: 100%">
      <el-button type="primary" :icon="CirclePlus" @click="add">新增</el-button>
      <el-button type="danger" :icon="Remove" @click="Show" v-if="!isShow">批量删除</el-button>
      <el-button type="danger" :icon="Remove" @click="deleteBatch" v-if="isShow">删除</el-button>
      <el-button type="danger" :icon="CircleClose"  @click="Show" v-if="isShow">取消</el-button>
      <el-upload     :action="'http://localhost:9090/user/import'"   :on-success="handleUploadSuccess"   :show-file-list=false     accept="xlsx"   style="display: inline-block; margin: 0 10px" >
        <el-button type="primary" :icon="Download">导入</el-button>
      </el-upload>
      <el-button :icon="Upload"  type="primary" @click="exportUser">导出</el-button>

      <el-input v-model="username" placeholder="请输入用户名" style="width: 15%;margin: 0 10px 0 50px" clearable/>
      <el-input v-model="nickname" placeholder="请输入昵称" style="width: 15%;" clearable/>
      <el-button type="primary" :icon="Search" style="margin-left: 10px" @click="load">查询</el-button>
      <el-button type="primary" :icon="Refresh" style="margin-left: 10px" @click="reset">重置</el-button>
    </div>
    <el-table :data="tableData"  :row-style="tableRowStyle" :cell-style="{'text-align':'center'}" :header-cell-style="{'text-align':'center'}"
              border resizable="true" style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column v-if="isShow" type="selection" width="55"></el-table-column>
      <el-table-column prop="id" label="ID" sortable/>
      <el-table-column prop="userName" label="用户名"/>
      <el-table-column prop="nickName" label="昵称"/>
      <el-table-column prop="age" label="年龄"/>
      <el-table-column prop="sex" label="性别"/>
      <el-table-column prop="address" label="地址"/>
      <el-table-column prop="role" label="角色"/>
      <el-table-column fixed="right" label="操作" width="300px">
        <template #default="scope">
          <el-button type="primary" :icon="Edit" @click="handleEdit(scope.row)">编辑
          </el-button>
          <el-popconfirm title="确定要删除吗?" @confirm="handleDelete(scope.row.id)">
            <template #reference>
              <el-button :icon="Remove" type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin: 10px 0">
      <el-dialog
          v-model="dialogVisible"
          title="提示"
          width="30%"
      >
        <el-form label-width="120px">
          <el-form-item :model="form" label="用户名">
            <el-input v-model="form.userName" style="width: 80%"></el-input>
          </el-form-item>
          <el-form-item :model="form" label="昵称">
            <el-input v-model="form.nickName" style="width: 80%"></el-input>
          </el-form-item>
          <el-form-item :model="form" label="年龄">
            <el-input v-model="form.age" style="width: 80%"></el-input>
          </el-form-item>
          <el-form-item :model="form" label="角色">
            <el-input v-model="form.role" style="width: 80%"></el-input>
          </el-form-item>
          <el-form-item :model="form" label="性别">
            <el-radio v-model="form.sex" label="男">男</el-radio>
            <el-radio v-model="form.sex" label="女">女</el-radio>
            <el-radio v-model="form.sex" label="未知">未知</el-radio>
          </el-form-item>
          <el-form-item :model="form" label="地址">
            <el-input type="textarea" v-model="form.address" style="width: 80%"></el-input>
          </el-form-item>
        </el-form>
        <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="save">确定</el-button>
      </span>
        </template>
      </el-dialog>
    </div>
  </el-card>
  <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[2,5,10,20]"
      :page-size="pageSize"
      layout="total,sizes,prev,pager,next,jumper"
      :total="total">
  </el-pagination>
</div>
</template>
<script>
import request from "@/utils/request";
  import {getListAPI, getUserList} from "@/api/api";
import { Edit,Remove,CirclePlus,Upload,Download,Search,Refresh,View,CircleClose } from '@element-plus/icons-vue';
import {ElMessage} from "element-plus";
export default {
  setup(){
    return{
      Edit,
      Remove,
      CirclePlus,
      Upload,
      Download,
      Search,
      Refresh,
      View,
      CircleClose
    }
  },
  name: 'Home',
  components: {Edit},
  data() {
    return {
      form: {},
      dialogVisible: false,
      currentPage: 1,
      total: 0,
      pageSize: 10,
      username:"",
      nickname: "",
      tableData: [],
      bookList:[],
      bookVis:false,
      isShow:false,
      ids:[],
      roles:[],

    }
  },
  created() {
    this.load();
  },
  methods: {
    tableRowStyle({ row, rowIndex }) {
      return { background:'#F2F6FC', color: '#0052d9', 'font-weight': 600 }
    },
    reset(){
      this.username="";
      this.nickname="";
      this.load()
    },
    Show(){
      this.isShow=!this.isShow
    },
    deleteBatch(){
      if(!this.ids.length){
        this.$message.warning("请选择数据!")
        return
      }
      request.post("/user/del/batch",this.ids).then(res=>{
        if(res){
          this.$message.success("批量删除成功")
          this.load()
        }else{
          this.$message.error("批量删除失败")
        }
      })
    },
    handleSelectionChange(val){
      console.log(this.ids)
      this.ids=val.map(v => v.id)
    },
    handleUploadSuccess(res) {
      console.log(res)
      if (res.code === 200) {
        this.$message.success("导入成功")
        this.load()
      }
    },
    exportUser() {
      location.href = "http://localhost:9090/user/export";
    },



    load() {
      const params= {
        pageNum: this.currentPage,
            pageSize: this.pageSize,
            username: this.username,
      }
      getUserList(params).then(res => {

        this.tableData = res.records;
        this.total = res.total
      })




    },
    add() {
      this.dialogVisible = true;
      this.form = {}
    },
    save() {
      if (this.form.id) {
        request.post("/user/add", this.form).then(res => {
          console.log(res);
          if (res.code === 200) {
            ElMessage({
              type: "success",
              message: "更新成功"
            })
          } else {
            ElMessage({
              type: "error",
              message: "更新失败"
            })
          }
          this.load();
          this.dialogVisible = false
        });
      } else {
        request.post("/user/add", this.form).then(res => {
          console.log(res);
          if (res.code === 200) {
            ElMessage({
              type: "success",
              message: "新增成功"
            })
          } else {
            ElMessage({
              type: "error",
              message: "新增失败"
            })
          }
          this.load();
          this.dialogVisible = false
        })
      }
    },
    handleEdit(row) {
      this.form = JSON.parse(JSON.stringify(row));
      this.dialogVisible = true
    },
    handleSizeChange(pageSize) {
      this.pageSize=pageSize;
      this.load();
    },
    handleCurrentChange(pageNum) {
      this.currentPage=pageNum;
      this.load();
    },
    handleDelete(id){

      request.delete("/user/"+id).then(res => {
        console.log(res.code)
        if (res){
          ElMessage({
            type: "success",
            message: "删除成功"
          })
        }else {
          ElMessage({
            type: "error",
            message: "删除失败"
          })
        }
        this.load()//重新加载
      });
    }
  }
}
</script>
